package com.ymatou.query.seller.trading.api.parameter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.google.gson.JsonArray;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.database.sqlwapper.YmtOrdersWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.YMTDateUtil;

/**
 * sp_trd_GetSellerHistoryOrdersList
 * 
 * @author songyefeng
 *
 */
public class Sp_GetSellerHistoryOrdersList {
	private YmtOrdersWapper ymtOrdersWapper = new YmtOrdersWapper();
	private DefaultWapper defaultWapper = new DefaultWapper();

	private Integer sellerId;
	private Integer activityId;
	private String time;
	private Integer orderEstablishStatus;
	private String lastProductId;
	private List<Integer> orderStatus;
	private Integer top;
	private Boolean considerRiskVerfiedStatus;
	private Boolean considerOrderStatus;
	private Boolean considerRCOrderEstablish;
	private Boolean considerRCAccountPaid;
	private Boolean considerRestOrderStatus;

	public Integer getSellerId() {
		return sellerId;
	}

	public void setSellerId(Integer sellerId) {
		this.sellerId = sellerId;
	}

	public Integer getActivityId() {
		return activityId;
	}

	public void setActivityId(Integer activityId) {
		this.activityId = activityId;
	}

	public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}

	public Integer getOrderEstablishStatus() {
		return orderEstablishStatus;
	}

	public void setOrderEstablishStatus(Integer orderEstablishStatus) {
		this.orderEstablishStatus = orderEstablishStatus;
	}

	public String getLastProductId() {
		return lastProductId;
	}

	public void setLastProductId(String lastProductId) {
		this.lastProductId = lastProductId;
	}

	public List<Integer> getOrderStatus() {
		return orderStatus;
	}

	public void setOrderStatus(List<Integer> orderStatus) {
		this.orderStatus = orderStatus;
	}

	public Integer getTop() {
		return top;
	}

	public void setTop(Integer top) {
		this.top = top;
	}

	public Boolean getConsiderRiskVerfiedStatus() {
		return considerRiskVerfiedStatus;
	}

	public void setConsiderRiskVerfiedStatus(Boolean considerRiskVerfiedStatus) {
		this.considerRiskVerfiedStatus = considerRiskVerfiedStatus;
	}

	public Boolean getConsiderOrderStatus() {
		return considerOrderStatus;
	}

	public void setConsiderOrderStatus(Boolean considerOrderStatus) {
		this.considerOrderStatus = considerOrderStatus;
	}

	public Boolean getConsiderRCOrderEstablish() {
		return considerRCOrderEstablish;
	}

	public void setConsiderRCOrderEstablish(Boolean considerRCOrderEstablish) {
		this.considerRCOrderEstablish = considerRCOrderEstablish;
	}

	public Boolean getConsiderRCAccountPaid() {
		return considerRCAccountPaid;
	}

	public void setConsiderRCAccountPaid(Boolean considerRCAccountPaid) {
		this.considerRCAccountPaid = considerRCAccountPaid;
	}

	public Boolean getConsiderRestOrderStatus() {
		return considerRestOrderStatus;
	}

	public void setConsiderRestOrderStatus(Boolean considerRestOrderStatus) {
		this.considerRestOrderStatus = considerRestOrderStatus;
	}

	@SuppressWarnings("rawtypes")
	public GetSellerHistoryOrdersListResponse Execute() {
		GetSellerHistoryOrdersListResponse response = new GetSellerHistoryOrdersListResponse();
		List<SellerHistoryOrder> orderList = new ArrayList<SellerHistoryOrder>();
		if (lastProductId != null) {
			String queryField = " top 1 o.dAddTime";
			StringBuffer joinCondition = new StringBuffer();
			joinCondition
					.append(" join Ymt_OrderInfo(nolock) i on o.iOrderId = i.iOrderId");
			joinCondition
					.append(" join Ymt_OrderInfoExt(nolock) e on i.sOrderInfoId = e.sOrderInfoId and e.iActivityId != "
							+ activityId);
			String queryCondition = " o.iBuyerId = " + sellerId
					+ " and i.sProductId = '" + lastProductId
					+ "' order by o.dAddTime desc";
			List<Map> temp = ymtOrdersWapper.selectOrders2(queryField,
					joinCondition.toString(), queryCondition);
			if (temp.size() > 0) {
				if (temp.get(0).get("dAddTime") != null) {
					time = String.valueOf(temp.get(0).get("dAddTime"));
				}
			}
		}

		String queryField = " oi.sProductId, o.dAddTime,";
		queryField += " case when o.iTradingStatus =" + orderEstablishStatus
				+ "  then oi.iAmount else 0 end as notPaidCount,";
		queryField += " case when isnull(o.iTradingStatus, 0) != "
				+ orderEstablishStatus
				+ " then oi.iAmount else 0 end as soldCount";
		String joinCondition = " join Ymt_Orders(nolock) o on o.iOrderId = oi.iOrderId";
		joinCondition += " join Ymt_OrderInfoExt(nolock) e on oi.sOrderInfoId = e.sOrderInfoId and e.iActivityId != "
				+ activityId;
		StringBuffer queryCondition = new StringBuffer();
		queryCondition.append(" o.iBuyerId = " + sellerId);
		queryCondition.append(" and o.bShangouOrder = 1");
		queryCondition.append(" and o.dAddTime < '" + time + "'");
		if (DataUtil.isNullOrEmpty(orderStatus) == false) {
			queryCondition.append(" and o.iTradingStatus in "
					+ DataUtil.toInOfSql(orderStatus));
		}
		List<Map> results = defaultWapper.selectOrderInfo(queryField,
				joinCondition, queryCondition.toString());
		List<SellerHistoryOrder> orders = new ArrayList<SellerHistoryOrder>();
		if (results != null) {
			for (Map m : results) {
				SellerHistoryOrder order = new SellerHistoryOrder();
				order.setLastOrderTime(String.valueOf(m.get("dAddTime")));
				order.setNotPaidNum(DataUtil.Integerr.GetValueOrDefault(m
						.get("notPaidCount")));
				order.setSaleNum(DataUtil.Integerr.GetValueOrDefault(m
						.get("soldCount")));
				order.setProductId((String) m.get("sProductId"));
				orders.add(order);
			}
		}
		Map<String, List<SellerHistoryOrder>> groupByPid = orders
				.stream()
				// .filter(x -> x.getProductId() != null)
				.collect(
						Collectors.groupingBy(SellerHistoryOrder::getProductId));
		Iterator<String> it = groupByPid.keySet().iterator();
		List<SellerHistoryOrder> orderList2 = new ArrayList<SellerHistoryOrder>();
		while (it.hasNext()) {
			SellerHistoryOrder order = new SellerHistoryOrder();
			String key = it.next();
			List<SellerHistoryOrder> orders2 = groupByPid.get(key);
			order.setProductId(key);
			order.setLastOrderTime(orders2.stream().max(
					(x, y) -> YMTDateUtil.parseDate(x.getLastOrderTime(),
							YMTDateUtil.YMDHMSS).compareTo(
							YMTDateUtil.parseDate(y.getLastOrderTime(),
									YMTDateUtil.YMDHMSS))).get().getLastOrderTime());
			int soldCount = 0;
			int notPaidCount = 0;
			for (SellerHistoryOrder o : orders2) {
				soldCount += o.getSaleNum();
				notPaidCount += o.getNotPaidNum();
			}
			order.setSaleNum(soldCount);
			order.setNotPaidNum(notPaidCount);
			orderList2.add(order);
		}
		orderList2.sort((a, b) -> YMTDateUtil.parseDate(b.getLastOrderTime(),
				YMTDateUtil.YMDHMSS)
				.compareTo(
						YMTDateUtil.parseDate(a.getLastOrderTime(),
								YMTDateUtil.YMDHMSS)));
		if (top >= orderList2.size()) {
			orderList = orderList2;
		} else {
			for (int i = 0; i < top; i++) {
				orderList.add(orderList2.get(i));
			}
		}
		response.setOrderList(orderList);
		return response;
	}
}
